//
//  NCImageView.m
//  BaiduNews
//
//  Created by neusoft on 11-7-25.
//  Copyright 2011 Neusoft. All rights reserved.
//

#import <QuartzCore/QuartzCore.h>
#import "NCEmoticonView.h"
#import "NCEmoticonViewPage.h"
#import "BtDefines.h"
#import "EmoticonUtils.h"

#define kScrollViewFame     CGRectMake(0,5,320,185)
#define kPageViewFame1      CGRectMake(0,0,320,185)
#define kPageViewFame2      CGRectMake(320,0,320,185)
#define kPageControlFame    CGRectMake(150,190,20,10)

//GrayPageControl.m：
//#import “GrayPageControl.h”
@implementation GrayPageControl

@synthesize lastPage = _lastPage;

-(id) init
{
    self = [super init];
    activeImage = [[UIImage imageNamed:@"image_dot.png"] retain];
    inactiveImage = [[UIImage imageNamed:@"image_dot_off.png"] retain];
    return self;
}

-(void) updateDots
{
    for (int i = 0; i < [self.subviews count]; i++)
    {
        UIImageView* dot = [self.subviews objectAtIndex:i];
        if (i == self.currentPage) dot.image = activeImage;
        else dot.image = inactiveImage;
    }
}

-(void) setCurrentPage:(NSInteger)page
{
    [super setCurrentPage:page];
    _lastPage = page;
    [self updateDots];
}

-(void)dealloc
{
    [super dealloc];
}
@end 


@implementation NCEmoticonView

@synthesize counterLabel = _counterLabel;


#pragma mark -
#pragma mark Override base functions
- (id)initWithFrame:(CGRect)frame
       withTextView:(UITextView*)textView
     withParentView:(UIView*)parentView
{
    if ((self = [super initWithFrame:frame])) {
		_textView   = textView;
        _parentView = parentView;
        _isShown = NO;
        self.backgroundColor = [UIColor clearColor];
        
        _emoticonViewPageArray = [[NSMutableArray alloc] init];
        
        _scrollView = [[UIScrollView alloc] initWithFrame:kScrollViewFame];
        _scrollView.scrollsToTop = YES;
        _scrollView.contentSize = CGSizeMake(640, 185);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;

        _scrollView.showsVerticalScrollIndicator = FALSE;
        _scrollView.showsHorizontalScrollIndicator = FALSE;

        [self addSubview: _scrollView];
        
        NSArray *emotionArray = [[[NSArray alloc] initWithArray:[EmoticonUtils getEmoticonTextArray]] autorelease];
        NSArray *firstPageEmoticonArray = [emotionArray subarrayWithRange:NSMakeRange(0,28)];
        NCEmoticonViewPage *emoticonView = [[[NCEmoticonViewPage alloc] initWithFaceArray:firstPageEmoticonArray
                                                                                withFrame:kPageViewFame1
                                                                             withDelegate:self] autorelease];
        emoticonView.frame = kPageViewFame1;
        [_emoticonViewPageArray addObject:emoticonView];
        _displayedPage = emoticonView;
        [_scrollView addSubview:emoticonView];
        
        NSArray *secondPageEmoticonArray = [emotionArray subarrayWithRange:NSMakeRange(28, [emotionArray count]-28)];
        emoticonView = [[[NCEmoticonViewPage alloc] initWithFaceArray:secondPageEmoticonArray
                                                            withFrame:kPageViewFame2
                                                         withDelegate:self] autorelease];
        emoticonView.frame = kPageViewFame2;
        [_emoticonViewPageArray addObject:emoticonView];
        [_scrollView addSubview:emoticonView];
        
        _pageControl = [[GrayPageControl alloc] init];
        _pageControl.enabled = NO;
        _pageControl.frame = kPageControlFame;
        _pageControl.numberOfPages = [_emoticonViewPageArray count];
        [_pageControl setCurrentPage:0];

                //---add the event handler for the page control---
        //[_pageControl addTarget:self action:@selector(pageTurning:)
        //      forControlEvents:UIControlEventValueChanged];
        [self addSubview:_pageControl];
    }
    
    return self;
}

- (id)initWithDelegate:(CGRect)frame
          withDelegate:(id)delegate
{
    if ((self = [self initWithFrame:frame withTextView:nil withParentView:nil])) 
    {
        _delegate = delegate;
    }
    return self;
}

- (void) setWritingViewFlag:(BOOL)flag
{
    _writingViewFlag = flag;
}

- (void) scrollViewDidScroll:(UIScrollView *)sender
{
    // 得到每页宽度
    CGFloat pageWidth = sender.frame.size.width;
    // 根据当前的x坐标和页宽度计算出当前页数
    int currentPage = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    
#ifdef PRINT_LOG 
    if (_writingViewFlag && _pageControl.lastPage != currentPage) 
    {

        LVAppDelegate *appDelegate = (LVAppDelegate *)[[UIApplication sharedApplication] delegate];
        [appDelegate formatLogStringData:eLogWritingEmotionAnotherPage];
    }
#endif
    
    [_pageControl setCurrentPage:currentPage];
}


- (void)show
{
    _isShown = YES;
    if(_textView)
    {
        [_textView resignFirstResponder];
    }
//    self.hidden = !_isShown;
}

- (void)close
{
    _isShown = NO;
    if(_textView)
    {
        [_textView becomeFirstResponder];
    }
//    self.hidden = YES;
}

-(BOOL)isShown
{
    return _isShown;
}

- (void)selectEmoticon:(NSString*)emoticonText
{
    [self close];
    int count = 0;
    if (_counterLabel)
    {
        if (_counterLabel && _counterLabel.text)
        {
            count = [_counterLabel.text intValue];
        }
    }
    if(_textView && count >= 3)
    {
        _textView.text = [NSString stringWithFormat:@"%@%@", _textView.text, emoticonText];
    }
    if (_delegate != nil && [_delegate respondsToSelector:@selector(emoticonSelected:)])
	{
		[_delegate emoticonSelected:emoticonText];
	}
}

- (void)dealloc {
    [_emoticonViewPageArray release];
    [_scrollView release];
    [_pageControl release];
    [super dealloc];
}

@end
